namespace com.tc.zigbee.mt
{

    using System.Collections.Generic;


    using com.tc.gateway.device;
    using com.tc.gateway;
    using com.tc.zigbee.entity;
    using com.tc.zigbee.entity.device;
    using com.tc.zigbee.mt.request;
    using com.tc.zigbee.zcl;
    using com.tc.zigbee.ha;

    public class ZigbeeGroupManager : GroupManager
    {
        ZigbeeManager manager;

        public ZigbeeGroupManager(ZigbeeManager zm)
        {
            manager = zm;
        }
        public void addDevice(string deviceId, short groupId, string groupName)
        {
            ZigbeeDevice d = manager.getDevice(deviceId);

            Endpoint e = d.getEndpoint();
            byte[] c = ZclCommandCreator.addGroup(groupId, groupName == null ? "" : groupName,
                    (byte)1);
            MtCommand mc = RequestFactory.MtAfDataRequest((byte)2, e.getParent()
                    .getNwkAddress(),

            e.getEndpointId(), (byte)manager.getRemoteControl()
                    .getEndpointId(), (short)0x0004, (byte)0, (byte)0, (byte)2,
                    (byte)c.Length, c);
            RequestQueue.offer(mc);
        }
        public void storeScene(string deviceId, short groupId)
        {
            ZigbeeDevice d = manager.getDevice(deviceId);

            Endpoint e = d.getEndpoint();


            byte[] c = ZclCommandCreator.storeScene((byte)0, groupId, manager.nextSceneId());
            MtCommand mc = RequestFactory.MtAfDataRequest(
                    (byte)2,
                    e.getParent()
                    .getNwkAddress(),

                    e.getEndpointId(),
                    (byte)manager.getRemoteControl()
                    .getEndpointId(),
                    (short)ZclConstants.ZCL_CLUSTER_ID_GEN_SCENES,
                    (byte)0,
                    (byte)0,
                    (byte)2,
                    (byte)c.Length,
                    c);
            RequestQueue.offer(mc);
        }
        public List<Device> members(short group)
        {
            // TODO Auto-generated method stub
            return manager.getGroups()[group].getMembers();
        }
        public List<Device> notmembers(short group)
        {
            List<Device> all = manager.getDevicesProfile(HAConstants.ZCL_HA_PROFILE_ID);
            List<Device> ms = manager.getGroups()[group].getMembers();
            List<Device> rst = new List<Device>();
            foreach(Device  d in all){
                if (ms.Contains(d))
                    continue;
                rst.Add(d);

            }

            return rst;
        }
        public void remove(string deviceId, short groupId)
        {
            ZigbeeDevice d = manager.getDevice(deviceId);

            Endpoint e = d.getEndpoint();
            byte[] c = ZclCommandCreator.removeGroupMember((byte)1, groupId);
            MtCommand mc = RequestFactory.MtAfDataRequest((byte)2, e.getParent()
                    .getNwkAddress(),

            e.getEndpointId(), (byte)manager.getRemoteControl()
                    .getEndpointId(), (short)0x0004, (byte)0, (byte)0, (byte)2,
                    (byte)c.Length, c);
            RequestQueue.offer(mc);

        }
        public List<Group> getAll()
        {
            // TODO Auto-generated method stub
            return new List<Group>(manager.getGroups().Values);
        }
        public Group addGroup()
        {
            ZigbeeGroup g = new ZigbeeGroup();
            g.setId(manager.nextGroupId());
            g.setName("m");


            manager.getGroups().Add(g.getId(), g);
            return g;
        }
        public Group get(short gid)
        {
            // TODO Auto-generated method stub
            return manager.getGroups()[gid];
        }

    }
}
